<?php namespace App\Http\Controllers\user\uc\devicetech;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use Session;
use Input;
use App\Models\Projects;
use App\Models\Devices;
use App\Models\Technologies;
use App\Models\FinancialIncoming;
use App\Models\Financing;
use App\Models\Liability;
use App\Models\CashFlow;

use App;
use Illuminate\Pagination\LengthAwarePaginator;
use Landish\Pagination\Pagination as BootstrapPresenter;

class FinanceController extends Controller {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		$uid = $this->getUid();
		$device = Devices::with(['finance'])->where("uid", $uid)->where("is_financing", 1)->get()->toArray();
		$techs = Technologies::with(['finance'])->where("uid", $uid)->where("is_financing", 1)->get()->toArray();

		return view('user.uc.devicetech.finance', ["device" => $device, "techs" => $techs])->withDeviceFinance(true);
	}

	public function editFinance()
	{
		return view("user.uc.devicetech.edit_finance")->withDeviceFinance(true);
	}

	public function financeDetail()
	{
		return view("user.uc.devicetech.finance_detail")->withDeviceFinance(true);
	}

	public function getFinanceType()
	{
		$type = Input::get("type");
		$uid = $this->getUid();
		switch($type)
		{
			case 1:
				$data = Projects::where("uid", $uid)->where("is_financing", 0)->where("verify_state", 1)->get()->toArray();
				break;
			case 2:
				$data = Devices::where("uid", $uid)->where("is_financing", 0)->where("verify_state", 1)->get()->toArray();
				break;
			case 3:
				$data = Technologies::where("uid", $uid)->where("is_financing", 0)->where("verify_state", 1)->get()->toArray();
				break;
			default :
				$data = [];
		}

		return $this->json(["data" => $data]);
	}


	public function addFinance()
	{
		$type 		= Input::get("type_id");
		$related_id = Input::get("relate_id");
		$report 	= Input::get("report");
		$imgArr[] 	= $report['canbe_report_file'];
		$imgArr[] 	= $report['prove_report_file'];
		$imgArr[] 	= $report['gov_agreement_file'];
		$this->moveImages($imgArr, env("OTHERS_DIR"));
		$report['related_id'] = $related_id;
		$report['type'] = $type;
		$report 	= Financing::create($report);
		$lirun 		= Input::get("lirun");
		$lirun["financing_id"] = $report->id;
		FinancialIncoming::create($lirun);
		$fuzai 		= Input::get("fuzai");
		$fuzai["financing_id"] = $report->id;
		Liability::create($fuzai);
		$liuliang 	= Input::get("liuliang");
		$liuliang["financing_id"] = $report->id;
		CashFlow::create($liuliang);
		if($type == 2)
		{
			$model = Devices::where("id", $related_id);
			$model->update(["is_financing" => 1]);
		}
		if($type == 3)
		{
			$model = Technologies::where("id", $related_id);
			$model->update(["is_financing" => 1]);
		}

		return $this->json([]);
	}


	public function editFinanceDetail()
	{
		return view("user.uc.devicetech.update_finance")->withDeviceFinance(true);
	}

	public function getFinance()
	{
		$finid = Input::get("finid");
		$related_id = Financing::where("id", $finid)->pluck('related_id');
		$type = Financing::where("id", $finid)->pluck('type');
		switch ($type)
		{
			case env("FINANCING_PROJECT"):
				$puid = Projects::where("id", $related_id)->pluck("uid");
				break;
			case env("FINANCING_DEVICE"):
				$puid = Devices::where("id", $related_id)->pluck("uid");
				break;
			case env("FINANCING_TECH"):
				$puid = Technologies::where("id", $related_id)->pluck("uid");
				break;
		}
		$uid = $this->getUid();
		if($uid != $puid){
			return;
		}
		$data = Financing::with(["finIncome", "liability", "cashFlow"])->where("id", $finid)->get()[0]->toArray();
		return $this->json(["data" => $data]);
	}


	public function updateFinance()
	{
		$finid = Input::get("finid");
		$related_id = Financing::where("id", $finid)->pluck('related_id');
		$type = Financing::where("id", $finid)->pluck('type');
		switch ($type)
		{
			case env("FINANCING_PROJECT"):
				$puid = Projects::where("id", $related_id)->pluck("uid");
				break;
			case env("FINANCING_DEVICE"):
				$puid = Devices::where("id", $related_id)->pluck("uid");
				break;
			case env("FINANCING_TECH"):
				$puid = Technologies::where("id", $related_id)->pluck("uid");
				break;
		}
		$uid = $this->getUid();
		if($uid != $puid){
			return;
		}
		$financeData = Input::get("report");
		$incomeData = Input::get("lirun");
		$cashData = Input::get("liuliang");
		$liabilityData = Input::get("fuzai");
		$imgArr[] 	= $financeData['canbe_report_file'];
		$imgArr[] 	= $financeData['prove_report_file'];
		$imgArr[] 	= $financeData['gov_agreement_file'];
		$this->moveImages($imgArr, env("OTHERS_DIR"));
		$finance = Financing::where("id", $finid);
		$finance->update($financeData);
		$finIncome = FinancialIncoming::where("financing_id", $finid);
		$finIncome->update($incomeData);
		$liability = Liability::where("financing_id", $finid);
		$liability->update($liabilityData);
		$cashFlow = CashFlow::where("financing_id", $finid);
		$cashFlow->update($cashData);

		return $this->json([]);
	}

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}
}
